﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery drag
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.实现Menu，Accordion功能
2.支持多级Menu、html转换、数据绑定、自定义显示模板
3.支持menu的选择定位
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    ///#region 设置
    var dragSetting = {
        css: 'tabs'
            , csscontent: 'tab-content'
            , csscontentitem: 'tab-content-item'
    };
    ///#endregion

    $.fn.drag = function (options) {
        /// <summary>pager</summary>
        /// <param name="contents" type="JQSelector">Tab内容区域</param>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>               
        options = $.extend({
            handler: null,
            onMove: null,
            onDrop: null
        }, options);
        return this.each(function () {
            var ctr = $(this).css({ 'cursor': 'move' }), handler = options.handler || ctr,
             _ismove = false, _position;
            if(!options.handler)
                options.handler = ctr;
            ctr.mousedown(function (e) {
                var target = $(options.handler);
                _ismove = true;
                _position = $.getEventPosition(e);
            });
            $(document).mousemove(function (e) {
                if (_ismove) {
                    var mPosition = $.getEventPosition(e), movectr = $(options.handler),padding = 10,
                    ctrposition = movectr.getPosition(),
                    win = $(window), maxX = win.width() - movectr.width() - padding,
                    maxY = win.height() - movectr.height() - padding,
                    top = ctrposition.top + mPosition.y - _position.y,
                    left = ctrposition.left + mPosition.x - _position.x;
                    movectr.css({ top: getRangeValue(top, padding, maxY), left: getRangeValue(left, padding, maxX) });
                    _position = mPosition;
                    if(options.onMove)
                        options.onMove(movectr,e);
                }
            }).mouseup(function (e) {
                if(_ismove)
                {
                    _ismove = false;
                    if(options.onDrop)
                        options.onDrop(e);
                }
            })
            ;
        });
    };

    ///#region 公共方法
    function drag() {
        var dragData = e.data.dragData;
        dragData.target.css({
            left: dragData.left + e.pageX - dragData.offLeft,
            top: dragData.top + e.pageY - dragData.offTop
        });
        dragData.handler.css({ cursor: 'move' });
        dragData.onMove(e);
    }
    ///#endregion

    ///#region 公共设置方法
    ///#endregion

})(jQuery);